package org.example.controller;


import dto.InspectionQualityQueryDTO;
import dto.UserDTO;
import dto.UserQueryDTO;
import entity.Users;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;


import lombok.extern.slf4j.Slf4j;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.AdminService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@Validated
@Tag(name = "管理员模块", description = "管理员相关操作")
@Slf4j
public class AdminController {
    @Resource
    private AdminService adminService;

    //添加员工
    @PostMapping("/addUsers")
    @Operation(summary = "添加员工")
    @Transactional
    public Result addUsers(@RequestBody @Validated UserDTO user) {
        log.info("添加员工：{}", user);
        if(adminService.isUsernameExists(user.getUsername())) {
            return Result.error("用户名已存在");
        }
        adminService.addUser(user);
        return Result.success();
    }

    /**
     * 分页查询员工信息
     * @param pageNum
     * @param pageSize
     * @param username
     * @param department
     * @return
     */
    @GetMapping("/queryUsers")
    @Operation(summary = "分页查询员工信息")
    public Result<PageResult> queryUsers(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "username", required = false) String username,
            @RequestParam(name = "department", required = false) String department
    ) {
        log.info("分页查询员工信息：pageNum={}, pageSize={}, username={}, department={}", pageNum, pageSize, username, department);
        UserQueryDTO userQueryDTO = UserQueryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .username(username)
                .department(department)
                .build();
        PageResult pageResult = adminService.queryUsers(userQueryDTO);
        return Result.success(pageResult);
    }

    //修改员工信息
    @PutMapping("/updateUsers")
    @Operation(summary = "修改员工信息")
    public Result updateUsers(@RequestBody @Validated UserDTO user) {
        log.info("修改员工信息：{}", user);
        adminService.updateInfo(user);
        return Result.success();
    }

    //删除员工
    @DeleteMapping("/deleteUsers")
    @Operation(summary = "删除员工")
    public Result deleteUser(@RequestBody Users user) {
        log.info("删除员工：{}", user);
        if (adminService.deleteUser(user)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //重置密码
    @PutMapping("/resetPassword")
    @Operation(summary = "重置密码")
    public Result resetPassword(@RequestParam String userName) {
        log.info("重置密码：{}", userName);
        adminService.resetPassword(userName);
        return Result.success("密码重置成功");
    }

}